Sprite Byte: Adding a Scoreboard or Status Panel

Level: Beginner - Intermediate

by Alyce Watson [http://alycesrestaurant.com/]

Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index

Sprite Bytes are small tutorials that address a single method to be used with sprites in Liberty BASIC. See Sprite Bytes in previous newsletters for information on user-controlled sprites, scrolling backgrounds and shooting. Please see the zipped archive of this newsletter for all files needed to run the demo program.

This is an adaptation of the sprite program from issue #124. Please see that issue for details on creating the basic sprite shooting program.

Why we can't just print a score on the sprite graphicbox?

Graphics that are drawn on a sprite graphicbox or in a sprite window are covered each time a "drawsprites" command is issued. The frame of animation in memory covers everthing in the graphicbox. You can draw with graphics commands after each instance of the "drawsprites" command, if you want. The animation may flicker if you do this, and it might slow down, too.

What can we use instead?

If you use a graphicbox for sprites instead of a graphics window, you can use other controls on the window. I like to use a statictext control to hold scores and other game information. I usually place it just above the graphicbox or just below the graphicbox, like this one from the demo.

statictext #1.s, space$( 50) + "Shots Fired: 0",0,maxY+20,WindowWidth-5,36
graphicbox #1.g, 0,0,maxX,maxY+20

See the game screenshot below.

Customizing the look of the scoreboard.

It's very easy to make the statictext control blend with the game, using Liberty BASIC control color statements. The ForegroundColor$ statement sets the color of text on the statictext control. The BackgroundColor$ statement sets the background color of the statictext. Since the game has a black background and white stars, I used this in my demo:

BackgroundColor$="black"
ForegroundColor$="white"

Using this method, the statictext blends in with the game screen. For fun, I used STYLEBITS to add a border to the statictext to make the scoreboard stand out.

stylebits #1.s, _WS_THICKFRAME,0,0,0
statictext #1.s, space$( 50) + "Shots Fired: 0",0,maxY+20,WindowWidth-5,36

Janet will be sharing more statictext tricks with stylebits in future installments of the StyleBits Corner.

Displaying multiple kinds of information.

You can display multiple kinds of information on a statictext. You can do this by adding spaces between the different bits of data. The SPACE$(n) command allows you to add spaces quite easily. The following code places a string of text on the statictext control to update it with each new frame of animation. The string is built by adding the various elements together. This is called concatenation. When the string is ready, it is printed on the statictext control as a string variable called msg$. To keep the text from appearing at the absolute left side, the string of text begins with 50 blank spaces. It then documents the number of shots fired by the user. There is then a separation of 20 spaces. The time elapsed since the beginning of play is documented last. Notice that variables, such as the number of shots fired, must appear outside of the quotation marks, while string literals appear inside the quotation marks.

    gameTime=time$("seconds")-startTime
    msg$=space$(50) + "Shots Fired: " ; shotsFired
    msg$=msg$ + space$(20) + "   Time Elapsed: ";gameTime
    #1.s msg$ 

You can use this method to document as many things as necessary. You can also use more than one statictext control to get a fancy, custom display. Why not experiment with this technique and share your game with the online community at one of the forums?

DEMO

Be sure to download the zipped archive of this issue to get the bitmaps and wav file for this demo.

'move sprite and shoot
'uses spritetravelxy
'use window with graphicbox so statictext will show
'use BackgoundColor$ and ForegroundColor$ to match look
'keep track of shots fired and put notice on scoreboard

nomainwin
'ship is 40x40
'shot is 10x10

maxX=550 : maxY=350
shotsFired=0    'keep track of missiles fired
startTime=time$("seconds")

WindowWidth=maxX : WindowHeight=maxY+80

BackgroundColor$="black"
ForegroundColor$="white"

loadbmp "ship","ship.bmp"
loadbmp "shot","shoot.bmp"

stylebits #1.s, _WS_THICKFRAME,0,0,0
statictext #1.s, space$( 50) + "Shots Fired: 0",0,maxY+20,WindowWidth-5,36
graphicbox #1.g, 0,0,maxX,maxY+20
open "Shooting Multiple Missiles" for window_nf as #1
    #1 "trapclose [quit]"

    'create a black background with stars:
    #1.g "down; fill black; color white"
    for i = 1 to 200
        x = int(rnd(0)*maxX)
        y = int(rnd(0)*maxY)
        #1.g "set ";x;" ";y
    next
    #1.g "getbmp bkg 0 0 ";maxX;" ";maxY

    'set the background
    #1.g "background bkg"

    'add your sprites
    #1.g "addsprite ship ship"

    'add 10 identical missiles
    'all use same loaded bmp
    'concatenate sprite names
    for i = 1 to 10
        #1.g "addsprite shot"+str$(i)+" shot"
    next

    'set ship location
    shipX=100 : shipY=maxY-20
    #1.g "spritexy ship ";shipX;" ";shipY

    'set location of all missiles off screen
    shotX=-100 : shotY=-100
    for i = 1 to 10
        #1.g "spritexy shot"+str$(i)+" ";shotX;" ";shotY
    next
    activeShot=0    'var to hold number of most recently fired missile

    bgX=0:bgY=0

    'show frame onscreen
    #1.g "drawsprites"

    'set up to trap keypresses
    #1.g "setfocus; when characterInput [check]"

[mainLoop]
    SCAN    'scan for user events
    gosub [moveship]

    gameTime=time$("seconds")-startTime
    msg$=space$(50) + "Shots Fired: " ; shotsFired
    msg$=msg$ + space$(20) + "   Time Elapsed: ";gameTime
    #1.s msg$ 

    'slight pause to slow action:
    calldll #kernel32, "Sleep",10 as long, re as void
    goto [mainLoop]


[check]
    k$=Inkey$   'place value of Inkey$ into k$
    if k$="4" or asc(right$(k$,1))=37 then gosub [goLeft]
    if k$="6" or asc(right$(k$,1))=39 then gosub [goRight]
    if k$="8" or asc(right$(k$,1))=38 then gosub [goShoot]
    goto [mainLoop]

[goLeft]
    #1.g "spritexy? ship x y"
    if x <= 0 then wait   'ship is at left side already
    shipX = shipX - 5
    return

[goRight]
    #1.g "spritexy? ship x y"
    if x >= maxX then wait   'ship is at right side already
    shipX = shipX + 5
    return

[goShoot]
    shotsFired=shotsFired+1
    activeShot=activeShot+1
    if activeShot>10 then activeShot=1
    #1.g "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY
    'set shot to travel to top of screen and
    'fire event when it gets there
    #1.g "spritexy shot"+str$(activeShot)+" ";shotX;" ";shotY
    #1.g "spritetravelxy shot"+str$(activeShot)+" ";shotX;"  0 5 [checkShoot]"
    playwave "laser.wav", async
    return

[checkShoot]
    for i = 1 to 10
        #1.g "spritexy? shot"+str$(i)+" sx sy"
        if sy<=0 then
            'move shot sprite off screen, update display
            #1.g "spritexy shot"+str$(i)+" -100 -100"
        end if
    next
    #1.g "drawsprites"    'update screen!
    goto [mainLoop]

[moveship]
    'put sprites in new location
    bgY=bgY-2   'scroll background
    #1.g "backgroundxy ";bgX;" ";bgY
    #1.g "spritexy ship ";shipX;" ";shipY
    #1.g "drawsprites"    'update screen!
    RETURN

[quit]
    unloadbmp "bkg"
    unloadbmp "ship"
    unloadbmp "shot"
    close #1:end                                                                                                        


Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index